/* 
 * Copyright (c) 2009, Giora Kosoi giorak@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Giora Kosoi ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Giora Kosoi BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sparrow/variant_string.h>
#include <sparrow/errors.h>

VariantString::VariantString()
    :   Variant(VT_STR)
{
}

VariantString::VariantString(const std::string &value)
    :   Variant(VT_STR)
{
    str(value);
}

VariantString::VariantString(const Octets &value)
    :   Variant(VT_STR)
{
    octets(value);
}

VariantString::VariantString(const uint8_t *value, size_t value_len)
    :   Variant(VT_STR)
{
    octets(value, value_len);
}

VariantString::VariantString(const Octets::const_iterator &begin, const Octets::const_iterator &end)
    :   Variant(VT_STR)
{
    octets(begin, end);
}

VariantString::~VariantString()
{
}

const Octets &VariantString::octets() const
{
    if(o_.empty())
    {
        o_.assign(s_.begin(), s_.end());
    }
    return o_; 
}

void VariantString::octets(const uint8_t *value, size_t value_len)
{
    s_.assign(value, value + value_len);
    o_.clear();
}

void VariantString::octets(const Octets &value)
{
    octets(&value.at(0), value.size());
}

void VariantString::octets(const Octets::const_iterator &begin, const Octets::const_iterator &end)
{
    octets(&(*begin), end - begin);
}

const std::string &VariantString::str() const
{
    if(s_.empty())
    {
        s_.assign(o_.begin(), o_.end());
    }
    return s_;
}

const std::string &VariantString::pstr() const
{
    if(ps_.empty())
    {
        ps_.insert(ps_.size(), "\"");
        ps_.insert(ps_.size(), str());
        ps_.insert(ps_.size(), "\"");
    }
    return ps_;
}

void VariantString::str(const std::string &value)
{
    s_ = value;
    o_.clear();
}

void VariantString::swap(std::string &value)
{
    s_.swap(value);
    o_.clear();
}

void VariantString::swap(Octets &value)
{
    s_.clear();
    o_.swap(value);
}

Variant *VariantString::clone() const
{
    return new VariantString(s_);
}



